package id.ac.itb.BubbleBlast2Solver;

import id.ac.itb.GenericSolver.AbstractState;
import id.ac.itb.GenericSolver.AbstractStep;
import id.ac.itb.GenericSolver.Solver;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Collection;

public class BubbleBlast2Solver {
	
	enum Algorithm {
		BFS,
		DFS,
		IDS
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Algorithm mode;
		if (args.length < 2) return;
		
		if (args[0].equals("-dfs")) {
			mode = Algorithm.DFS;
		} else if (args[0].equals("-ids")) {
			mode = Algorithm.IDS;
		} else {
			mode = Algorithm.BFS;
		}
		
		try {
			for (int i=1; i<args.length; i++) {
				System.out.println("--- " + args[i]);
				
				FileInputStream fis = new FileInputStream(args[i]);
				Board board = Board.createBoard(fis);
				Board duplicate = new Board(board);
				long startTime = 0, endTime = 0;
				
				System.out.println(board);
				Solver.nStatus = 0;
				switch (mode) {
				case BFS:
					System.out.println("Mode            = BFS");
					startTime = System.currentTimeMillis();
					Solver.BFS(board);
					endTime = System.currentTimeMillis();
					break;
				case DFS:
					System.out.println("Mode            = DFS");
					startTime = System.currentTimeMillis();
					Solver.DFS(board, new ArrayList<AbstractState>());
					endTime = System.currentTimeMillis();
					break;
				case IDS:
					System.out.println("Mode            = IDS");
					startTime = System.currentTimeMillis();
					Solver.IDS(board);
					endTime = System.currentTimeMillis();
					break;
				}

				Collection<AbstractStep> sols = board.getFirstSolutionSteps();
				System.out.println("Total Time      = " + (endTime - startTime) + " ms");
				System.out.println("Total Problem Status = " + Solver.nStatus);
				System.out.println("Solution Depth  = " + sols.size());
				System.out.println();
				
				int stepIdx = 1;
				for (AbstractStep step : sols) {
					System.out.println(stepIdx + ". " + step);
					duplicate.plop(((Plop) step).getRow(), ((Plop) step).getCol());
					
					if (stepIdx == 1 || stepIdx == 2 || stepIdx == sols.size())
					System.out.println(duplicate);
					stepIdx++;
				}
				
				System.out.println();
				System.out.println();
			}
        } catch (Exception ex) {
            ex.printStackTrace();
        }
	}

}
